﻿#include "SymbolInfoView.h"
#include <QTimer>
#include <QUrl>
#include <QList>
#include <QMimeData>
#include "SymbolDetail.h"
#include "FileOperator.h"
#include "Tools.h"
#include "FrameData.h"
#include "ThostFtdcUserApiDataType.h"

SymbolInfoView::SymbolInfoView(const QString &pFile, QWidget *parent)
    : QMainWindow(parent)
	, m_curFileName(pFile)
{
    ui.setupUi(this);

	setAcceptDrops(true);
	ui.table_symbolInfos->setAcceptDrops(true);
	QTimer::singleShot(10, this, SLOT(readFile()));
}

void SymbolInfoView::readFile()
{
	if (m_curFileName.isEmpty())
	{
		return;
	}
	setWindowTitle(m_curFileName);
	if (m_curFileName.endsWith("frmpec"))
	{
		readFramePrice(m_curFileName);
	}
	//else if (m_curFileName.endsWith("frmklin"))
	//{
	//	readFrameKItems(m_curFileName);
	//}
	else if (m_curFileName.endsWith("klin"))
	{
		readKItems(m_curFileName);
	}
	else if (m_curFileName.endsWith("pce"))
	{
		readMarketData(m_curFileName);
	}
	else if (m_curFileName.endsWith("symbols"))
	{
		readSymbol(m_curFileName);
	}
	
}

void SymbolInfoView::dragEnterEvent(QDragEnterEvent *e)
{
	if (e->mimeData()->hasFormat("text/uri-list")) //只能打开文本文件
		e->acceptProposedAction(); //可以在这个窗口部件上拖放对象
}

void SymbolInfoView::dropEvent(QDropEvent *event)
{
	QList<QUrl> urls = event->mimeData()->urls();
	m_curFileName = urls.first().toLocalFile();
	readFile();
}

void SymbolInfoView::readSymbol(const QString &filePath)
{
	ui.table_symbolInfos->clear();
	QStringList colNames;
	colNames << QString::fromWCharArray(L"交易所代码") << QString::fromWCharArray(L"合约代码") << QString::fromWCharArray(L"合约数量乘数") << QString::fromWCharArray(L"最小变动单位") << QString::fromWCharArray(L"产品类型") << QString::fromWCharArray(L"合约名称") << QString::fromWCharArray(L"多头保证金率") << QString::fromWCharArray(L"空头保证金率") << QString::fromWCharArray(L"市价单最大下单量") << QString::fromWCharArray(L"市价单最小下单量") << QString::fromWCharArray(L"限价单最大下单量") << QString::fromWCharArray(L"限价单最小下单量") << QString::fromWCharArray(L"交割年份") << QString::fromWCharArray(L"交割月");
	ui.table_symbolInfos->setColumnCount(colNames.size());
	//ui.table_market->setHorizontalHeaderItem(0, new QTableWidgetItem());
	ui.table_symbolInfos->setHorizontalHeaderLabels(colNames);
	ui.table_symbolInfos->setColumnWidth(1, 200);

	SymbolPackage::SymbolsReader symbolReader(filePath.toLocal8Bit().data(), true);
	std::list<SymbolPackage::SymbolInfo> pSymbolList;
	symbolReader.GetAllFutureList(pSymbolList);
	ui.table_symbolInfos->setRowCount(pSymbolList.size());
	int pindex = 0;
	for (std::list<SymbolPackage::SymbolInfo>::iterator it = pSymbolList.begin(); it != pSymbolList.end(); ++it, ++pindex)
	{
		
		ui.table_symbolInfos->setItem(pindex, 0, createTabItem(it->ExchangeID));
		ui.table_symbolInfos->setItem(pindex, 1, createTabItem(it->InstrumentID));
		ui.table_symbolInfos->setItem(pindex, 2, createTabItem(it->VolumeMultiple));
		ui.table_symbolInfos->setItem(pindex, 3, createTabItem(it->PriceTick));
		ui.table_symbolInfos->setItem(pindex, 4, createTabItem(it->ProductClass));
		ui.table_symbolInfos->setItem(pindex, 5, createTabItem(it->InstrumentName));
		ui.table_symbolInfos->setItem(pindex, 6, createTabItem((float)it->LongMarginRatio));
		ui.table_symbolInfos->setItem(pindex, 7, createTabItem((float)it->ShortMarginRatio));
		ui.table_symbolInfos->setItem(pindex, 8, createTabItem(it->MaxMarketOrderVolume));
		ui.table_symbolInfos->setItem(pindex, 9, createTabItem(it->MinMarketOrderVolume));
		ui.table_symbolInfos->setItem(pindex, 10, createTabItem(it->MaxLimitOrderVolume));
		ui.table_symbolInfos->setItem(pindex, 11, createTabItem(it->MinLimitOrderVolume));
		ui.table_symbolInfos->setItem(pindex, 12, createTabItem(it->DeliveryYear));
		ui.table_symbolInfos->setItem(pindex, 13, createTabItem(it->DeliveryMonth));
	}
}

void SymbolInfoView::readMarketData(const QString &filePath)
{
	ui.table_symbolInfos->clear();
	QStringList colNames;
	colNames << QString::fromWCharArray(L"时间") << QString::fromWCharArray(L"毫秒") << QString::fromWCharArray(L"价格") << QString::fromWCharArray(L"持仓量") << QString::fromWCharArray(L"成交量");
	//ui.table_market->setHorizontalHeaderItem(0, new QTableWidgetItem());
	ui.table_symbolInfos->setColumnCount(colNames.size());
	ui.table_symbolInfos->setHorizontalHeaderLabels(colNames);
	ui.table_symbolInfos->setColumnWidth(0, 200);

	FileOperator::FileBin fileDb(filePath.toStdString().c_str(), "rb");
	if (fileDb.FileInstance() == nullptr)
	{
		return;
	}
	ui.table_symbolInfos->setRowCount(0);
	fseek(fileDb.FileInstance(), 0L, SEEK_END);
	int len = ftell(fileDb.FileInstance());
	rewind(fileDb.FileInstance());
	int ksz = len / sizeof(MData::PriceItem);
	MData::PriceItem pItem;
	ui.table_symbolInfos->setRowCount(ksz);
	for (int i = 0; i < ksz; ++i)
	{
		fread(&pItem, sizeof(MData::PriceItem), 1, fileDb.FileInstance());
		int nrow = i;
		ui.table_symbolInfos->setItem(nrow, 0, createTimeTabItem(pItem.UpdateTime));
		ui.table_symbolInfos->setItem(nrow, 1, createTabItem(pItem.UpdateMillisec));
		ui.table_symbolInfos->setItem(nrow, 2, createTabItem(pItem.LastPrice));
		ui.table_symbolInfos->setItem(nrow, 3, createTabItem(pItem.OpenInterest));
		ui.table_symbolInfos->setItem(nrow, 4, createTabItem(pItem.Volume));
	}
}

void SymbolInfoView::readKItems(const QString &filePath)
{
	ui.table_symbolInfos->clear();
	QStringList colNames;
	colNames << QString::fromWCharArray(L"时间") << QString::fromWCharArray(L"毫秒") << QString::fromWCharArray(L"高") << QString::fromWCharArray(L"低") << QString::fromWCharArray(L"开") << QString::fromWCharArray(L"收");
	ui.table_symbolInfos->setColumnCount(colNames.size());
	ui.table_symbolInfos->setHorizontalHeaderLabels(colNames);
	ui.table_symbolInfos->setColumnWidth(1, 200);

	FileOperator::FileBin fileDb(filePath.toStdString().c_str(), "rb");
	if (fileDb.FileInstance() == nullptr)
	{
		return;
	}

	fseek(fileDb.FileInstance(), 0L, SEEK_END);
	int len = ftell(fileDb.FileInstance());
	rewind(fileDb.FileInstance());
	int ksz = len / sizeof(MData::KLineItem);
	MData::KLineItem pItem;
	ui.table_symbolInfos->setRowCount(ksz);
	for (int i = 0; i < ksz; ++i)
	{
		fread(&pItem, sizeof(MData::KLineItem), 1, fileDb.FileInstance());
		//ui.table_symbolInfos->setItem(i, 0, createIndexTabItem(pItem.Index));
		ui.table_symbolInfos->setItem(i, 0, createTimeTabItem(pItem.UpdateTime));
		ui.table_symbolInfos->setItem(i, 1, createTabItem(pItem.UpdateMillisec));
		ui.table_symbolInfos->setItem(i, 2, createTabItem(pItem.High));
		ui.table_symbolInfos->setItem(i, 3, createTabItem(pItem.Low));
		ui.table_symbolInfos->setItem(i, 4, createTabItem(pItem.Open));
		ui.table_symbolInfos->setItem(i, 5, createTabItem(pItem.Close));
	}
}

void SymbolInfoView::readFramePrice(const QString &filePath)
{
	ui.table_symbolInfos->clear();
	QStringList colNames;
	colNames << QString::fromWCharArray(L"合约名称") << QString::fromWCharArray(L"时间") << QString::fromWCharArray(L"毫秒") << QString::fromWCharArray(L"价格");
	//ui.table_market->setHorizontalHeaderItem(0, new QTableWidgetItem());
	ui.table_symbolInfos->setColumnCount(colNames.size());
	ui.table_symbolInfos->setHorizontalHeaderLabels(colNames);
	ui.table_symbolInfos->setColumnWidth(0, 200);

	//FData::PriceStream pStream;
	//std::list<FData::PriceItem> pricelist;
	//pStream.Get(pricelist);
	//ui.table_symbolInfos->setRowCount(pricelist.size());
	//int rowIndex = 0;
	//for (std::list<FData::PriceItem>::iterator it = pricelist.begin(); it != pricelist.end(); ++it, ++rowIndex)
	//{
	//	ui.table_symbolInfos->setItem(rowIndex, 0, createTabItem(it->SymbolName));
	//	ui.table_symbolInfos->setItem(rowIndex, 1, createTabItem(it->PItem.UpdateTime));
	//	ui.table_symbolInfos->setItem(rowIndex, 2, createTabItem(it->PItem.UpdateMillisec));
	//	ui.table_symbolInfos->setItem(rowIndex, 3, createTabItem(it->PItem.LastPrice));
	//}
}

//void SymbolInfoView::readFrameKItems(const QString &filePath)
//{
//	ui.table_symbolInfos->clear();
//	QStringList colNames;
//	colNames << QString::fromWCharArray(L"合约名称") << QString::fromWCharArray(L"索引") << QString::fromWCharArray(L"时间") << QString::fromWCharArray(L"高") << QString::fromWCharArray(L"低") << QString::fromWCharArray(L"开") << QString::fromWCharArray(L"收");
//	ui.table_symbolInfos->setColumnCount(colNames.size());
//	ui.table_symbolInfos->setHorizontalHeaderLabels(colNames);
//	ui.table_symbolInfos->setColumnWidth(1, 200);
//
//	FData::KItemStream iStream;
//	std::list<FData::KLineItem> itemList;
//	iStream.Get(itemList);
//	ui.table_symbolInfos->setRowCount(itemList.size());
//	int rowIndex = 0;
//	for (std::list<FData::KLineItem>::iterator it = itemList.begin(); it != itemList.end(); ++it, ++rowIndex)
//	{
//		ui.table_symbolInfos->setItem(rowIndex, 0, createTabItem(it->SymbolName));
//		ui.table_symbolInfos->setItem(rowIndex, 1, createTabItem(it->KItem.Index));
//		ui.table_symbolInfos->setItem(rowIndex, 2, createTabItem(it->KItem.UpdateTime));
//		ui.table_symbolInfos->setItem(rowIndex, 3, createTabItem(it->KItem.High));
//		ui.table_symbolInfos->setItem(rowIndex, 4, createTabItem(it->KItem.Low));
//		ui.table_symbolInfos->setItem(rowIndex, 5, createTabItem(it->KItem.Open));
//		ui.table_symbolInfos->setItem(rowIndex, 6, createTabItem(it->KItem.Close));
//	}
//}

QTableWidgetItem * SymbolInfoView::createTabItem(float pVal)
{
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::number(pVal, 10, 3));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createTabItem(unsigned int pVal)
{
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::number(pVal));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createTimeTabItem(time_t ptmVal)
{
	std::string tmStr = Tools::DateTimeStamp2Str(ptmVal);
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::fromStdString(tmStr));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createTabItem(char ptmVal[])
{
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::fromLocal8Bit(ptmVal));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createTabItem(int pVal)
{
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::number(pVal));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createTabItem(char ProductClass)
{
	QString ptext;
	switch (ProductClass)
	{
	case THOST_FTDC_PC_Futures:
		ptext = QString::fromWCharArray(L"期货");
		break;
	case THOST_FTDC_PC_Options:
		ptext = QString::fromWCharArray(L"期货期权");
		break;
	case THOST_FTDC_PC_Combination:
		ptext = QString::fromWCharArray(L"组合");
		break;
	case THOST_FTDC_PC_Spot:
		ptext = QString::fromWCharArray(L"即期");
		break;
	case THOST_FTDC_PC_EFP:
		ptext = QString::fromWCharArray(L"期转现");
		break;
	case THOST_FTDC_PC_SpotOption:
		ptext = QString::fromWCharArray(L"现货期权");
		break;
	case THOST_FTDC_PC_TAS:
		ptext = QString::fromWCharArray(L"TAS合约");
		break;
	case THOST_FTDC_PC_MI:
		ptext = QString::fromWCharArray(L"金属指数");
		break;
	default:
		break;
	}
	QTableWidgetItem *pItem = new QTableWidgetItem(ptext);
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}

QTableWidgetItem * SymbolInfoView::createIndexTabItem(long long pVal)
{
	QTableWidgetItem *pItem = new QTableWidgetItem(QString::number(pVal));
	pItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	return pItem;
}
